var pageSize = 10;

//时间戳格式化
function formatDateTime(inputTime , type){
    type = type||6;
    if(inputTime < 9999999999) inputTime = inputTime * 1000; //如果以秒为单位,则转为以毫秒为单位
    var date = new Date(inputTime);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;

    var res_arr = [
        y , m , d , h , minute , second
    ];
    var res = '';
    var s = '';
    for(var i=0; i<type; i++) {
        if(i == 0) s = '';
        else if(i < 3) s = '-';
        else if(i < 4) s = ' ';
        else s = ':';
        res += s + res_arr[i];
    }
    return res;
    //return y + '-' + m + '-' + d+' '+h+':'+minute +':'+second;
}

function closeIframe() {
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index); //再执行关闭
}


function tableLoginNo(res, curr, count) {
    //渲染页面时候.是ajax操作.当过期时候,执行跳转,data里面是登录url
    if(res.code == 10010) location.href=res.data;
}

function tableRender(cols,url,page,limit,elem,id,done, treeConfig) {
    layui.use('table',function() {

        var table = layui.table;
        var loading = layer.msg("数据请求中", {icon: 16, time: -1,}); //出现加载框
        elem = elem || '#test';
        id = id || 'list';
        if(page === true || "undefined" == typeof page) page = {
            elem: page,
            layout: ['prev', 'page', 'next', 'last', 'skip'] //自定义分页布局
            , groups: 10 //只显示 5 个连续页码
            , prev: '上一页'
            , next: '下一页'
            , first: 1 //不显示首页
            //,last: true //不显示尾页
        };

        limit = limit || pageSize;
        var done = done || tableLoginNo;
        const tableConfig = {
            elem: elem
            //,height: 350 ,整个表格的高度
            ,url: url
            //,size: 'sm'
            ,page:page
            ,limit: limit
            //,toolbar: '#toolbarDemo'
            ,cols: cols
            ,id:id
            ,autoSort: false
            ,done:function(res, curr, count) {
                loading && layer.close(loading); //关闭加载框
                done(res, curr, count);
            }
            ,loading:true
        }
        if(!treeConfig) {
            table.render(tableConfig);
        } else {
            const tableTree = layui.tableTreeDj
            tableTree.render(tableConfig, treeConfig)
        }

    });

}

//结束时间默认时分秒
var search_time_add_end = {};
function layDateDone() {
    lay.extend(search_time_add_end.config.dateTime, {
        hours: 23
        ,minutes: 59
        ,seconds: 59
    });
}

//常规表单验证相关
function lengthCheckNum(value,num) {
    if(value.length > num) return '长度不能超过' + num;
}

function lengthCheck120(value) {
    return lengthCheckNum(value, 120);
}

function lengthCheck30(value) {
    return lengthCheckNum(value,30);
}

function lengthCheck10(value) {
    return lengthCheckNum(value,10);
}

//验证字符串是否包含特殊符号
function strHaveSymbol(str) {
    var regEn = /[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/im,
        regCn = /[·！#￥（——）：；“”‘、，|《。》？、【】[\]]/im;

    if(regEn.test(str) || regCn.test(str)) {
        return '不能包含特殊字符';
    }
}


function createMenu($, data, id) {
    if(!id) id = 'left_tab';
    var itemedClass = 'layui-nav-itemed';
    var html = '';
    var htmlChildren = '';
    var dataChildren = {};  //子菜单
    var dataParent = []; //一级菜单
    //整理子菜单与父菜单
    $.each(data,function(i,item) {
        if(item.pid != 0) {
            if(!dataChildren[item.pid]) dataChildren[item.pid] = [];
            dataChildren[item.pid].push(item);
        } else dataParent.push(item);
    });

    //循环父菜单,并赋值
    $.each(dataParent,function(i,item) {
        if(i != 0) itemedClass = '';
        if(!dataChildren[item.id]) {
            //没有子菜单
            html = '<li class="layui-nav-item ' + itemedClass + '">' +
                '<a target="' +item.target + '" href="' + item.url + '">' + item.name + '</a>' +
                '</li>';
        } else {
            //有子菜单
            htmlChildren = '';
            $.each(dataChildren, function(i,itemChild) {
                if(i == item.id) {
                    $.each(itemChild, function(i,itemLine) {
                        htmlChildren = htmlChildren + '<dd><a target="' +itemLine.target + '" href="'+ itemLine.url +'">'+ itemLine.name +'</a></dd>';
                    });
                }
            });
            html = '<li class="layui-nav-item layui-nav-itemed">' +
                    '<a class="" href="javascript:;">'+ item.name +'<span class="layui-nav-more"></span></a>' +
                    '<dl class="layui-nav-child">' +
                        htmlChildren +
                    '</dl>' +
                '</li>';
        }
        $('#'+id).append(html);
    });
}

function p(data) {
    console.log(data);
}

layui.use('jquery', function() {
    var $ = layui.jquery;
    $(document).ajaxError(function(){
        layer.msg('程序错误');
    });
});